<?php

namespace App\Exports;


use App\Models\User\User;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

class DemoExport extends CommonExport
{
    public function __construct(array $conditions=[],array $params=[]) {
        $this->instance = (new User());
        $this->conditions = $conditions;
        $this->params = $params;
        $this->title = [
            'ID',
            '姓名',
            '护照号',
            '电话',
            '生日',
            '积分',
            '备注',
            '创建时间',
        ];
    }

    public function array(): array {
        $this->records[] = $this->title;
        //$cols = $this->getAllCols();//获取所有字段

        $records = $this->instance->getLst($this->conditions)->get();

        foreach ($records as $record) {
            $temp = [
                $record->id,
                $record->name,
                $record->passport,
                $record->phone,
                $record->birthday,
                $record->point,
                $record->beizhu,
                $record->created_at,
            ];

            $this->records[] = $temp;
        }
        $this->records[] = ['导出时间:'.date('Y-m-d H:i:s')];
        return $this->records;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }

}
